package com.example.demo.service.impl;


import com.example.demo.domain.Grade;
import com.example.demo.domain.Student;
import com.example.demo.mapper.StudentMapper;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

@Service
public class StudentServiceImpl implements StudentService {

    private StudentMapper studentMapper;

    @Autowired
    public void setStudentMapper(StudentMapper studentMapper){
        this.studentMapper = studentMapper;
    }

    /**
     * 查询功能
     * @return
     */
    @Override
    public List<Student> findAll(Integer pageNum, Integer pageSize) {
        return studentMapper.findAll(pageNum,pageSize);
    }

    /**
     * 删除功能
     * @param id
     * @return
     */
    @Override
    public boolean deleteStudent(int id) {

        int row = studentMapper.deleteStudent(id);

        if(row > 0){
            return true;
        }
        return false;
    }

    /**
     * 插入功能
     * @param student
     * @return
     */
    @Override
    public boolean insertStudent(Student student) {
        student.setImg(getRandomImg());
        int row = studentMapper.insertStudent(student);
        if(row > 0){
            return true;
        }
        return false;
    }

    /**
     * 根据id查询功能
     * @param cId
     * @return
     */
    @Override
    public Student findStudentById(int cId) {
        return studentMapper.findStudentById(cId);
    }

    /**
     * 修改功能
     * @param student
     * @return
     */
    @Override
    public boolean updateStudent(Student student) {
        int row = studentMapper.updateStudent(student);
        if(row > 0){
            return true;
        }
        return false;
    }

    /**
     * 查询总记录数
     * @return
     */
    @Override
    public Integer findTotalRecord() {
        return studentMapper.findTotalRecord();
    }

    /**
     * 查询学生详细信息
     * @param id
     * @return
     */
    @Override
    public Student findByIdDetail(Integer id) {
        return studentMapper.findByIdDetail(id);
    }

    /**
     * 查询学生成绩
     * @param id
     * @return
     */
    @Override
    public List<Grade> findByIdWithGrade(Integer id) {
        return studentMapper.findByIdWithGrade(id);
    }




    /**
     *  放在CDN上的随机头像
     */
    private static final String[] headImg = {
            "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/12.jpeg",
            "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/11.jpeg",
            "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/13.jpeg",
            "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/14.jpeg",
            "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/15.jpeg",

    };
    private String getRandomImg(){
        int size = headImg.length;
        Random random = new Random();
        int index = random.nextInt(size);
        return headImg[index];
    }
}
